using System;
using System.Collections.Generic;
using Android.App;
using Android.Views;
using Android.Widget;
using Android.Util;
using static Android.Widget.CompoundButton;

namespace Markdone.Droid
{
    internal class TaskItemAdapter : BaseAdapter<TaskItem>
    {
        List<TaskItem> items = new List<TaskItem>();
        Activity context;
        //Dictionary<int, EventHandler<CheckedChangeEventArgs>> onCheckedChanges = new Dictionary<int, EventHandler<CheckedChangeEventArgs>>();
        public List<TaskItem> GetItems()
        {
            return this.items;
        }

        public override int Count
        {
            get
            {
                return items.Count;
            }
        }

        public override TaskItem this[int position]
        {
            get
            {
                return items[position];
            }
        }

        public TaskItemAdapter(Activity context, List<TaskItem> tasks) : base()
        {
            this.context = context;
            this.items = tasks;
        }

        public override long GetItemId(int position)
        {
            return position;
        }

        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            var view = convertView;
            if (convertView == null)
            {
                //Log.Error("ddddd", "inflate=");
                view = context.LayoutInflater.Inflate(Resource.Layout.TaskItem, null);
                SubscribeEvents(view, position);  // this make CheckedChange duplicated
            } 
            var item = items[position]; 
            PopulateView(view, item, position);
           
            return view;
        }

        public void PopulateView(View view, TaskItem item, int position)
        {
            var titleText = view.FindViewById<TextView>(Resource.Id.textViewTitle);
            var stateCB = view.FindViewById<CheckBox>(Resource.Id.checkBoxState);
            var starButton = view.FindViewById<ImageButton>(Resource.Id.imageButtonStar);
            var textClock = view.FindViewById<TextView>(Resource.Id.textViewDateTime);
            var repeatImage = view.FindViewById<ImageView>(Resource.Id.imageViewRepeat);

            // for event subscribers refer position
            stateCB.Tag = position;
            starButton.Tag = position;
            
            titleText.Text = item.Title;
            stateCB.Checked = item.State;
            starButton.Selected = item.IsStar;
            textClock.Text = string.Format("{0:yyyy-MM-dd ddd HH:mm}", item.DateTime);
            repeatImage.Visibility = item.IsRepeat ? ViewStates.Visible : ViewStates.Invisible;

        }

        public void SubscribeEvents(View view, int position)
        {
            var stateCB = view.FindViewById<CheckBox>(Resource.Id.checkBoxState);
            var starButton = view.FindViewById<ImageButton>(Resource.Id.imageButtonStar);

            //stateCB.Tag = position;
            // how to unsubsribe the former one subscriber
            //stateCB.CheckedChange -= StateCB_CheckedChange;
            stateCB.CheckedChange += StateCB_CheckedChange; 

            //starButton.Tag = position;
            //starButton.Click -= StarButton_Click;
            starButton.Click += StarButton_Click;
               
                
 
        }

        private void StateCB_CheckedChange(object sender, CheckedChangeEventArgs e)
        {
            var position = (int)((CheckBox)sender).Tag;

            var item = items[position];
            Log.Error("DDD", "CheckedChange at: " + position.ToString() + " IsChecked: " + e.IsChecked.ToString());
            item.State = e.IsChecked;
        }

        private void StarButton_Click(object sender, EventArgs e)
        {
            var starButton = (ImageButton)sender;
            var position = (int)starButton.Tag;

            var item = items[position];

            Log.Error("DDD", "StarButton at: " + position.ToString() + " IsStar: " + item.IsStar.ToString());
            starButton.Selected = !item.IsStar;
            item.IsStar = starButton.Selected;
            
        }

    }
}